package com.crpc.core.config;

import cn.hutool.core.util.ObjectUtil;
import com.crpc.core.compress.Compressor;
import com.crpc.core.compress.CompressorFactory;
import com.crpc.core.loadBalance.LoadBalance;
import com.crpc.core.serialize.Serializer;
import com.crpc.core.serialize.SerializerFactory;
import com.crpc.core.spi.SpiHandler;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

@Slf4j
public class SpiResolver implements Resolver {

    public void loadConfigurationFromSpi(Configuration configuration) {
        List<ObjectWrapper<LoadBalance>> loadBalanceList = SpiHandler.getList(LoadBalance.class);
        if (ObjectUtil.isNotNull(loadBalanceList) && ObjectUtil.isNotEmpty(loadBalanceList)) {
            configuration.setLoadBalance(loadBalanceList.get(0).getObjectImpl());
        }

        List<ObjectWrapper<Compressor>> compressorList = SpiHandler.getList(Compressor.class);
        if (ObjectUtil.isNotNull(compressorList) && ObjectUtil.isNotEmpty(compressorList)) {
            CompressorFactory.addCompressorList(compressorList);
            configuration.setCompressor(compressorList.get(0).getObjectImpl());
        }

        List<ObjectWrapper<Serializer>> serializerList = SpiHandler.getList(Serializer.class);
        if (ObjectUtil.isNotNull(serializerList) && ObjectUtil.isNotEmpty(serializerList)) {
            SerializerFactory.addSerializerList(serializerList);
            configuration.setSerializer(serializerList.get(0).getObjectImpl());
        }

    }
}
